<?php

/**
 * 
 * 兑奖列表
 * @author alang
 */

class api_exchange_list_mdl extends Base_Controller
{
	
	
	public function __construct()
	{
		parent::__construct();
	}
	
	/**
	 * 
	 * 输出
	 * @param Array $data
	 */
	public function outputdata($data)
	{
		header('Content-Type:text/html; charset=utf-8', false);
		$exchange = array();
		$exchange = $this->get_exchange_list($data['QUESTNUM'],$data['QBEFORE'],intval($data['TIMEID']),$data['USERID']);
	
		$exchangedata = array();
		if(!empty($exchange))
		{
			foreach($exchange as $key => $v)
			{
				$tmp['EXCHANGEID'] = $v['id'];
				$tmp['DROWNO'] = $v['rewardid'];
				$tmp['DROWTITLE'] = $v['infos'];
				$tmp['PRIZENAME'] = $v['productname'];
				$tmp['IMAGEURL'] = site_url('/upload/productimg/').'/'.$v['img'];
				$tmp['PRIZETYPE'] = $v['ptype'];
				$tmp['EXCHANGESTATE'] = intval($v['exchangestate']);
				$tmp['SHIPDATE'] = $v['shipdate'];
				$tmp['SHIPADDRE'] = $v['address'];
				$tmp['TRACENUMBER'] = $v['tracenumber'];
				$tmp['EXPRESSOR'] = $v['expressor'];
				$tmp['RECVNAME'] = $v['recvname'];
				$tmp['TICKETCODE'] = $v['cardnum'];
				$tmp['RNUM'] = $v['rnum'];
				$tmp['PRODUCTID'] = $v['stockid'];
				$exchangedata[] = $tmp;
			}
		}
		$outdata['TAG'] = $data['TAG'];
		$outdata['USERID'] = $data['USERID'];
		$outdata['EMAIL'] =$data['EMAIL'];
		$outdata['TOKEN'] = $data['TOKEN'];
		$outdata['EXCHANGEDATA'] = $exchangedata;
	
		echo json_encode($outdata);
	}
	
	/**
	 * 获取用户中奖列表
	 * @param int questnum 取条数
	 * @param string qbefore 取前面的数据还是后面的数据
	 * @param string timeid 从哪个位置开始取
	 */
	private function get_exchange_list($questnum,$qbefore,$timeid,$uid)
	{
		if(!empty($timeid))
		{
			if($qbefore == 'N'){
			
				$this->db->where('lottery.id >',$timeid);
			
			}else{
					
				$this->db->where('lottery.id <',$timeid);
				
			}
		}
			
		$result = $this->db->select('lottery.id,lottery.rnum,lottery.uid,lottery.rewardid,lottery.productid,lottery.stockid,lottery.createtime
									,product.productname,product.ptype,product.img
									,reward.infos,
									exchange.exchangestate,exchange.shipdate,exchange.address,exchange.tracenumber,exchange.expressor
									,exchange.recvname,exchange.expressor,
									,stock.cardnum')
							->from('cj_lottery as lottery')
							->join('cj_products as product','product.id=lottery.productid')
							->join('cj_reward as reward','reward.id=lottery.rewardid')
							->join('cj_exchange as exchange','exchange.lotteryid=lottery.id','left')
							->join('cj_stock as stock','lottery.stockid = stock.id')
							->where('lottery.uid',$uid)
							->order_by('lottery.createtime','DESC')
							->limit($questnum)
							->get()->result_array();
		return $result;
	
							

	}
	
}